import api from '@/pages/Customer/PointsWithdrawal/request';
import { DrawerForm, ProFormSelect } from '@ant-design/pro-components';
import { Button, Form } from 'antd';
import { useMemo, useState } from 'react';

export type FormConfig = { Component: any; props: Record<string, any> };
export interface Props {
  data?: PointsWithdrawalApi.ListResponse;
  onSuccess?: () => void;
  onCancel?: () => void;
}
function PointsWithdrawalStatusForm(props: Props) {
  const { data } = props;

  const [form] = Form.useForm();
  const [formData, setFormData] = useState<any>({});
  const [info, setInfo] = useState<Partial<PointsWithdrawalApi.InfoResponse>>({});

  const options = useMemo(
    () => [
      { label: '未审核', value: 0 },
      { label: '已通过', value: 1 },
      { label: '已拒绝', value: 2 },
    ],
    [],
  );

  const getInfo = async (params: PointsWithdrawalApi.InfoParams) => {
    const info = await api.getInfo(params);
    const data = { ...info };
    setInfo(data);
    form.setFieldsValue(data);
    setFormData(data);
  };

  const onOpenChange = (open: boolean) => {
    if (open) {
      if (data && data.id) {
        getInfo({ id: data.id });
      }
    } else {
      setInfo({});
      form.resetFields();
      setFormData({});
      props.onCancel?.();
    }
  };
  const onSubmit = async () => {
    const { withdrawStatus } = formData;
    const { id = '' } = info;

    const params: PointsWithdrawalApi.StatusParams = {
      id,
      withdrawStatus,
    };
    const data = await api.updateStatus(params);
    if (data.success) props.onSuccess?.();
    return data.success;
  };

  return (
    <DrawerForm
      autoFocusFirstInput
      title={`提现状态`}
      form={form}
      drawerProps={{ destroyOnClose: true }}
      resize={{
        maxWidth: window.innerWidth * 0.8,
        minWidth: 400,
        onResize() {},
      }}
      trigger={<Button type="link">更新状态</Button>}
      onOpenChange={onOpenChange}
      onFinish={onSubmit}
    >
      <ProFormSelect
        required
        name="withdrawStatus"
        label="提现状态"
        placeholder="请选择提现状态"
        options={options}
        fieldProps={{
          value: formData.withdrawStatus,
          onChange: (value) => {
            setFormData({ ...formData, withdrawStatus: value });
          },
        }}
      />
    </DrawerForm>
  );
}

export default PointsWithdrawalStatusForm;
